/*
 * (C) 1992 Simson Garfinkel and Associates, Inc.
 *
 * NeXTSTEP developers may freely use and redistribute this software as long
 * as credit is given to Simson Garfinkel and Associates.
 *
 * EXPORT RESTRICTIONS:
 *
 * You may not ship the source-code module des.c outside of the US or canada.
 *
 * You may ship a program which uses the des.o compiled module outside of the
 * United States to any type T or type V country as long as you do not provide
 * cryptographic services to the user in your program and you clearly
 * declare "commodity control number 5D11A" on your export declaration.
 *
 * Type T countries include all countries in the Western Hemisphere except Cuba.
 * Type V countries include all countries in the Eastern Hemisphere except
 * the previous communist block countries and the People's Republic of China,
 * Vietnam, Cambodia, and Laos.
 *
 * For further information, contact the Office of Export Control:
 *
 *	Bureau of Export Administration
 *	P.O. Box 273
 *	Washington, DC 20044
 *	202-377-2694
 */
 
#import <appkit/appkit.h>
#import "Decoder.h"
#import "Registration.h"

@implementation Decoder

- calc:sender
{
	struct	licenseString	ls;
	char		key[9];
	char 		ks[16][8];
	char		*cc = (char *)&ls;
	const char 	*keyString = [companyKeyCell stringValue];

	if(keyString[0]==0){
		NXRunAlertPanel(0,"You must specify a company key",0,0,0);
		return self;
	}


	/* Convert to a license token */
	hex_to_binary([licenseStringCell stringValue],&ls,sizeof(ls));

	/* Decrypt the token */
	asciiToKey([companyKeyCell stringValue],key);

	desinit(0);
	dessetkey(ks,key);
	dedes(ks,cc+2);
	dedes(ks,cc);

	if(checksum(&ls,sizeof(ls))){
		[validCell		setStringValue:"Not valid"];
		[productCodeCell 	setStringValue:""];
		[licenseTypeCell	setStringValue:""];
		[startDateCell		setStringValue:""];
		[endDateCell		setStringValue:""];
		[accessionNumberCell	setStringValue:""];
		[maxMachinesCell	setStringValue:""];
		return self;
	}

	[validCell		setStringValue:"Valid"];

	if(ls.flag & REG_SINGLEUSER){
		[licenseTypeCell	setStringValue:"Single user"];
	}
	else if(ls.flag & REG_DEMO){
		[licenseTypeCell	setStringValue:"Demo"];
	}
	else {
		[licenseTypeCell	setStringValue:"Network"];
	}

	[productCodeCell 	setIntValue:ls.product];

	if(ls.flag & REG_START){
		char	buf[256];

		sprintf(buf,"%d/%d",(ls.start >> 4),((ls.start) & 0xf) + 92);
		[startDateCell		setStringValue:buf];
	}
	else{
		[startDateCell		setStringValue:""];
	}

	if(ls.flag & REG_END){
		char	buf[256];

		sprintf(buf,"%d/%d",(ls.end >> 4),((ls.end) & 0xf) + 92);
		[endDateCell		setStringValue:buf];
	}
	else{
		[endDateCell		setStringValue:""];
	}

	[accessionNumberCell	setIntValue:
	 ls.num[0] << 16 | ls.num[1] << 8 | ls.num[2]];

	[maxMachinesCell setIntValue: ls.maxMachines[0] << 8 + ls.maxMachines[1]];

	return self;
}


@end
